﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyMvcStoreApplication.Models;
using MyMvcStoreApplication.Repositories;

namespace MyMvcStoreApplication.Controllers
{ 
    public class AlbumController : Controller
    {
        private AlbumRepository repository = new AlbumRepository();

        //
        // GET: /Album/

        public ViewResult Index()
        {
            return View(repository.GetAll());
        }

        //
        // GET: /Album/Details/5

        public ViewResult Details(int id)
        {
            return this.ReturnView(repository.Get(id)); 
        }

        //
        // GET: /Album/Create

        public ActionResult Create()
        {
            return ReturnView(new Album());
        } 

        //
        // POST: /Album/Create

        [HttpPost]
        public ActionResult Create(Album album)
        {
            if (ModelState.IsValid)
            {
                repository.SaveNew(album);
                return RedirectToAction("Index");  
            }
            return ReturnView(album);
        }
        
        //
        // GET: /Album/Edit/5
 
        public ActionResult Edit(int id)
        {
            return ReturnView(repository.Get(id));
        }

        //
        // POST: /Album/Edit/5

        [HttpPost]
        public ActionResult Edit(Album album)
        {
            if (ModelState.IsValid)
            {
                repository.SaveModify(album);
                
                return RedirectToAction("Index");
            }
            return ReturnView(album);
        }

        //
        // GET: /Album/Delete/5
 
        public ActionResult Delete(int id)
        {
            return ReturnView(repository.Get(id));
        }

        //
        // POST: /Album/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            repository.Delete(id);
            return RedirectToAction("Index");
        }

        private ViewResult ReturnView(Album album)
        {
            this.LoadCombos();

            return View(album);
        }

        private void LoadCombos()
        {
            ViewBag.ArtistasList = repository.ReadArtistas();
            ViewBag.GeneroList = repository.ReadGeneros();
        }

        protected override void Dispose(bool disposing)
        {
            repository.Dispose();
            base.Dispose(disposing);
        }
    }
}